using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mani.Security;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.SqlClient;

public partial class ActionsRoles : BasePage
{

    RuleProvider ruleProvider = new RuleProvider();

    public override string ActionName {
        get {
            return "vw_actionsroles";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadRoles();
            LoadActions();
        }
    }

    private void LoadActions() {
        ReadOnlyCollection<Action> actions = ruleProvider.GetActionsForRole(drpRoles.SelectedValue);
        gvActionRoles.DataSource = actions;
        gvActionRoles.DataBind();
        LoadNewActions();
    }

    private void LoadRoles()
    {
        drpRoles.DataSource = Roles.GetAllRoles();
        drpRoles.DataBind();

        for (int i = 0; i < drpRoles.Items.Count; i++) {
            if (drpRoles.Items[i].Text == "Administrator") {
                drpRoles.Items[i].Enabled = false;
            }
            else {
                drpRoles.SelectedIndex = i;
            }
        }
    }

    protected void gvActionRoles_RowDeleting(object sender, GridViewDeleteEventArgs e) {
        List<string> roles = new List<string>( new string[] {drpRoles.SelectedValue} );
        ruleProvider.DenyActionFromRoles(gvActionRoles.DataKeys[e.RowIndex].Value.ToString().ToLower(), roles.AsReadOnly());
        LoadActions();
    }

    protected void btnAdd_Click(object sender, EventArgs e) {
        if (String.IsNullOrEmpty(drpAccess.SelectedValue))
            return;

        List<string> roles = new List<string>( new string[] {drpRoles.SelectedValue} );
        ruleProvider.GrantActionToRoles(drpAccess.SelectedValue.ToLower(), roles.AsReadOnly());
        LoadActions();
    }

    protected void drpRoles_SelectedIndexChanged(object sender, EventArgs e) {
        LoadActions();
    }

    private void LoadNewActions() {
        drpAccess.Items.Clear();

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString);
        conn.Open();
        string q = "SELECT ActionId, ActionName, Description FROM aspnet_Actions";
        SqlCommand command = new SqlCommand(q, conn);
        command.CommandText = q;
        drpAccess.DataSource = command.ExecuteReader();
        drpAccess.DataTextField = "Description";
        drpAccess.DataValueField = "ActionName";
        drpAccess.DataBind();

        ReadOnlyCollection<Action> actions = ruleProvider.GetActionsForRole(drpRoles.SelectedValue);
        List<string> actionNames = new List<string>();
        foreach (Action action in actions) {
            actionNames.Add(action.Name);
        }

        for (int i = 0; i < drpAccess.Items.Count; i++) {
            if (actionNames.Contains(drpAccess.Items[i].Value)) {
                drpAccess.Items[i].Enabled = false;
            }
        }
    }
}
